<%@ Control Language="c#" AutoEventWireup="false" Inherits="FourRoads.Links.Controls.GroupTreeViewControl" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<%@ Register TagPrefix="CS" Namespace="CommunityServer.Controls" Assembly="CommunityServer.Controls" %>
<%@ Register TagPrefix="CP" Namespace="CommunityServer.ControlPanel.Controls" Assembly="CommunityServer.Web" %>
<%@ Register TagPrefix="CA" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %>
<script language="javascript">
<!--
var nodeLastSelected = null;
var nodeMoved = null;
var nodeNeedsResorted = false;
var newSectionNode;
var groupNodeSelected = null;
var newNodeSeq = 0;
window.top.location

// Splits the node's value and returns the SectionID part
function getSectionID(node)
{
	var splits = node.GetProperty('Value').split(":");
	
	if (splits != null)
		return splits[0];
		
	return node.GetProperty('Value');
}

function updateSectionNode(newName, isActive)
{
	var updateNode = window.<%=this.Tree.ClientObjectId%>.SelectedNode
	if(updateNode == null)
		window.location = window.location;
	
	if(isActive == 'True') //used for enabled
	{
		updateNode.SetProperty('ImageUrl', "folder_open.gif");
		updateNode.SetProperty('Checked', 1);
	}
	else
	{
		updateNode.SetProperty('ImageUrl', "folder.gif");
		updateNode.SetProperty('Checked', null);
	}
	
	updateNode.SetProperty('Text',newName);
	//updateNode.SaveState();
	window.<%=this.Tree.ClientObjectId%>.Render();
}
function treeContextMenu(treeNode, e)
{
	nodeLastSelected = treeNode;
	sectionID = getSectionID(treeNode);

	if (sectionID != -1)
	{
		if(treeNode.Checked == 1) //used for enabled
			window.<%=this.SectionEnabledContextMenu.ClientID%>.ShowContextMenu(treeNode, e, sectionID);
		else
			window.<%=this.SectionDisabledContextMenu.ClientID%>.ShowContextMenu(treeNode, e, sectionID);
	}
	else
	{
		window.<%=this.SectionRootContextMenu.ClientID%>.ShowContextMenu(treeNode, e, sectionID);
	}
		
	return true;
}

function nodeSelect(node)
{
	nodeLastSelected = node;
	if (getSectionID(node) > 0)
	{
		window.frames["sectionEditFrame"].location  = "IFrameHost.aspx?SectionID=" + getSectionID(node);
	}
	else
	{
		window.frames["sectionEditFrame"].location = "rootPageView.aspx";
	}
}

function nodeRename(sourceNode, newName)
{ 	
	newName = newName.replace(/^\s*|\s*$/g,"");
	if(newName.length == 0)
	{
		alert('<%= FourRoads.Links.Controls.ResourceManager.GetString("CP_ResLink_TreeView_BlankSectionName") %>');
		return false;
	}
	
	window.<%=this.Tree.ClientObjectId%>.SelectedNode.SetProperty('Text', newName);
	
	// Determine whether this is a new node (doesn't exist server-side yet) or an existing one
	var sectionID = getSectionID(sourceNode);
	
	//CA is allowing the user to edit the new section node so trap it here
	if (sectionID > 0)
	{
		window.location = window.location;
	}
	
	if (sectionID == 0)
	{
		window.<%=this.Tree.ClientObjectId%>.Render();
			
		GroupTreeViewControl.AddSection('<%= this.ClientID %>' , getSectionID(sourceNode.ParentNode) , newName, AddSectionCallBack);
		return false;
	}

  return true; 
}

function checkNodeIndex(node)
{
	if (nodeNeedsResorted == true && nodeMoved != null)
	{
		nodeNeedsResorted = false;
		index = nodeMoved.GetCurrentIndex();
		sectionID = getSectionID(nodeMoved);
		nodeMoved = null;
		
		ReorderForumOrGroup(sectionID, index);
	}
}

//## MOVE ##

function nodeMove(sourceNode, targetNode)
{
	nodeMoved = sourceNode;
	nodeNeedsResorted = false;

	sourceSectionID = getSectionID(sourceNode);
	targetSectionID = getSectionID(targetNode);
	
	//alert(sourceGroupID + ':' + sourceSectionID + ':' + targetGroupID + ':' + targetSectionID);
	if (sourceSectionID > 0)
	{
		if (targetNode == null)
		{
			return false;
		}

		MoveSection(sourceSectionID, targetSectionID);
	}
	
	return true; 
}

function MoveSection(sectionID, targetSectionID)
{
	GroupTreeViewControl.MoveSection('<%= this.ClientID %>',sectionID, targetSectionID, MoveSectionCallBack);
}

function MoveSectionCallBack(res)
{
	// Check if this is a section that needs the sort order changed
	if (res.value == "-1")
	{
		if (nodeMoved != null)
		{
			index = nodeMoved.GetCurrentIndex();
			sectionID = getSectionID(nodeMoved);
			nodeMoved = null;
		
			ReorderForumOrGroup(sectionID, index);
		}
	}
	else
	{
		var retVals = res.value.split("^");
		var sectionID = retVals[0];
		var newSectionID = retVals[1];
		
		if (sectionID == "-1")
		{
			var nodeMoved = window.<%=this.Tree.ClientObjectId%>.FindNodeById(newSectionID);
			//Undo the move sectionID contains the node id where the move orginated and 
			var previousParentSectionID = retVals[2]; //contains the node that needs moving back
			var name = nodeMoved.GetProperty('Text');
			nodeMoved.Remove();
			AddSectionNode(previousParentSectionID , newSectionID ,  name);		
			//its not unique
			alert("<%= FourRoads.Links.Controls.ResourceManager.GetString("CP_ResLink_SectionEdit_StatusUpdateError") %>");			
		}
		else
		{
			var nodeMoved = window.<%=this.Tree.ClientObjectId%>.FindNodeById(sectionID);
			nodeMoved.SetProperty('Value',sectionID);
			nodeMoved.SetProperty('ID',sectionID);
			nodeMoved.SetProperty('NavigateUrl', 'IFrameHost.aspx?SectionID=' + sectionID);
			nodeMoved.SetProperty('Target', "sectionEditFrame");

			window.<%=this.Tree.ClientObjectId%>.Render();
			
			var selectedNode = window.<%=this.Tree.ClientObjectId%>.SelectedNode;
			if (selectedNode != null && getSectionID(selectedNode) == sectionID)
			{
				nodeSelect(nodeMoved);
			}
		}
	}
}

//## MOVE REORDER ##
function ReorderForumOrGroup(sectionID, index)
{
	GroupTreeViewControl.ReorderForumOrGroup('<%= this.ClientID %>', sectionID, index, NoResponseCallBack);
}
function NoResponseCallBack(res)
{
	// Do Nothing
}

function AddSection(sectionID)
{	
	toggleButtons(false);

	if (sectionID == null)
	{
		sectionID = getSectionID(nodeLastSelected);
	}
		
	newNodeSeq = newNodeSeq + 1; //ensure unique key names
	var newSectID = '0:'+newNodeSeq;

	AddSectionNode(sectionID , newSectID, '')
	var newNode = window.<%=this.Tree.ClientObjectId%>.FindNodeById(newSectID);

	newNode.SetProperty('EditingEnabled', true);
	newNode.SetProperty('DraggingEnabled', false);
	newNode.SetProperty('DroppingEnabled', false);

	//newNode.SaveState();
	window.<%=this.Tree.ClientObjectId%>.Render();
	newNode.Edit();
}

function AddSectionCallBack(res)
{
	try
	{
		cleanupNewNode();
		if (res.value == '0')
		{
			alert("<%= CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_ResLink_SectionEdit_Settings_AppKey_DuplicateNameException") %>");
		}
		else if(res.value != '-1')
		{
			var retVals = res.value.split("^");
			var sectionID = retVals[0];
			var sectionName = retVals[1];
			var parentSecID = retVals[2];
			
			AddSectionNode(parentSecID , sectionID, sectionName)
			window.frames["sectionEditFrame"].location  = "IFrameHost.aspx?SectionID=" + sectionID;
		}
	}
	catch(ex)
	{
	}
	
	finally
	{
		toggleButtons(true);
	}
}
function cleanupNewNode()
{
	var newNode = window.<%=this.Tree.ClientObjectId%>.FindNodeById("0:"+newNodeSeq);
	window.<%=this.Tree.ClientObjectId%>.SelectedNode = null;
		//var parentNode = sourceNode.ParentNode;
	newNode.Remove();
	//newNode.SaveState();
	window.<%=this.Tree.ClientObjectId%>.Render();
}
function AddSectionNode(parentNodeID , sectionID, name)
{
	var selNode = window.<%=this.Tree.ClientObjectId%>.FindNodeById(parentNodeID);
	if(selNode != null)
	{
		var newSectionNode = new ComponentArt_TreeViewNode();

		newSectionNode.SetProperty('Text', name);
		newSectionNode.SetProperty('ImageUrl', "folder.gif");
		newSectionNode.SetProperty('Checked', null);
		newSectionNode.SetProperty('EditingEnabled', false);
		newSectionNode.SetProperty('DraggingEnabled', true);
		newSectionNode.SetProperty('DroppingEnabled', true);
		newSectionNode.SetProperty('ClientSideOnNodeSelect', "nodeSelect");
		newSectionNode.SetProperty('Selectable', true);
		newSectionNode.SetProperty('Value', sectionID);
		newSectionNode.SetProperty('ID', sectionID);
		selNode.AddNode(newSectionNode);
		window.<%=this.Tree.ClientObjectId%>.SelectedNode = newSectionNode;
		window.<%=this.Tree.ClientObjectId%>.Render();
		
		selNode.Expand();
		window.<%=this.Tree.ClientObjectId%>.Render();
	}
	else
	{
		window.location = window.location;
	}
	
}

//## END ADD ##

//## ENABLE SECTION TOGGLE ##

ChangeSectionEnabled
function ChangeSectionEnabled(sectionID )
{
	GroupTreeViewControl.ChangeSectionEnabled('<%= this.ClientID %>', sectionID, ChangeSectionEnabledCallBack);
}
function ChangeSectionEnabledCallBack(res)
{
	if(res.value == '-1') //not an admin
		return;

	var sectionID  = res.value;

	var changedNode = window.<%=this.Tree.ClientObjectId%>.FindNodeById(sectionID);
	if (changedNode != null)
	{
		if(changedNode.Checked == 1) //used for enabled
		{
			changedNode.SetProperty('ImageUrl', "deletedFolder.gif");
			changedNode.SetProperty('Checked', null);
		}
		else
		{
			changedNode.SetProperty('ImageUrl', "folder.gif");
			changedNode.SetProperty('Checked',  1);
		}
		//changedNode.SaveState();
		window.<%=this.Tree.ClientObjectId%>.Render();
		
		//update the iframe
		if(window.<%=this.Tree.ClientObjectId%>.SelectedNode != null && changedNode.ID == window.<%=this.Tree.ClientObjectId%>.SelectedNode.ID)
			nodeSelect(changedNode); 
	}
	
	return;
}

function toggleButtons(state)
{
	toggleVisibility('<%=this.DeleteButtonClientID  %>' , state);
	toggleVisibility('<%=this.AddSectionButtonClientID  %>' , state);
}

function toggleVisibility( targetId , state ){ 
  if (document.getElementById){ 
        target = document.getElementById( targetId ); 
           if (state == true){ 
              target.style.display = ""; 
           } else { 
              target.style.display = "none"; 
           } 
     } 
} 

//## DELETE ##
function DeleteSelected(sectionID)
{
	// If a GroupID was not passed, get it from the selected node
	if (sectionID == null)
	{
		if (window.<%=this.Tree.ClientObjectId%>.SelectedNode == null)
			return;
		else
			nodeID = window.<%=this.Tree.ClientObjectId%>.SelectedNode.ID;

		//var vals = nodeID.split(":");
		//sectionID = vals[0];
		sectionID= nodeID;
	}
	
	if( sectionID == '-1')
		return;
	
	//clear the iframe if we are deleting the current item
	if (window.<%=this.Tree.ClientObjectId%>.SelectedNode != null)
	{
		var theCurrentNode = window.<%=this.Tree.ClientObjectId%>.SelectedNode
		if(getSectionID(theCurrentNode) == sectionID)
			window.frames["sectionEditFrame"].location  = "IFrameHost.aspx?SectionID=0";
	}
		
	if( sectionID != '-1')
		DeleteSection(sectionID);
}

//## DELETE SECTION ##
function DeleteSection(sectionID)
{
	if(confirm('<%= FourRoads.Links.Controls.ResourceManager.GetString("CP_ResLInk_Delete_Warning") %>'))
		GroupTreeViewControl.DeleteSection('<%= this.ClientID %>', sectionID,  DeleteSectionCallBack);
}

function DeleteSectionCallBack(res)
{

	if (res.value == null)
	{
		alert('<%= FourRoads.Links.Controls.ResourceManager.GetString("CP_ResLink_GroupList_SectionDeleteException") %>');
		return;
	}
	
	if (res.value == "0")
	{
		alert('<%= FourRoads.Links.Controls.ResourceManager.GetString("CP_ResLink_GroupList_SectionDeleteException") %>');
		return;
	}
	
	//var retVals = res.value.split("^");
	//var sectionID = retVals[0];
	var sectionID = res.value;
	
	var deletedNode = window.<%=this.Tree.ClientObjectId%>.FindNodeById(sectionID);
	if (deletedNode != null)
	{
		deletedNode.Remove();
		window.<%=this.Tree.ClientObjectId%>.Render();
	}
		
	return;
}
//## END DELETE ##

//for splitter support
function resizeCategoryTree(DomElementId, NewPaneHeight, NewPaneWidth)
    {
      // Forces the treeview to adjust to the new size of its container 
		window.<%=this.Tree.ClientID%>.Render();
		if(DomElementId != null)
			resizeDetails();
    }

//-->
</script>
<cs:contextmenu filename="~/ControlPanel/ResLinkAdmin/SectionNodeEnabledContextMenu.config" runat="server"
	id="SectionEnabledContextMenu" onmenuopenscript="" onmenuclosescript="" />
<cs:contextmenu filename="~/ControlPanel/ResLinkAdmin/SectionNodeDisabledContextMenu.config" runat="server"
	id="SectionDisabledContextMenu" onmenuopenscript="" onmenuclosescript="" />
<cs:contextmenu filename="~/ControlPanel/ResLinkAdmin/SectionNodeRootContextMenu.config" runat="server"
	id="SectionRootContextMenu" onmenuopenscript="" onmenuclosescript="" />
<ca:treeview id="Tree" Width="100%" Height="100%" draganddropenabled="true" nodeeditingenabled="true"
	keyboardenabled="true" AllowTextSelection="true" cssclass="ForumFolderTree" nodecssclass="ForumFolderTreeNode"
	selectednodecssclass="ForumFolderTreeNodeSelected" hovernodecssclass="ForumFolderTreeNodeHover"
	lineimagewidth="19" lineimageheight="20" defaultimagewidth="16" defaultimageheight="16" ExpandCollapseImageWidth="17"
	ExpandCollapseImageHeight="15" itemspacing="0" imagesbaseurl="~/ControlPanel/Images/CATreeView/small_icons/"
	nodelabelpadding="0" parentnodeimageurl="folder.gif" expandedparentnodeimageurl="folder_open.gif"
	showlines="true" lineimagesfolderurl="~/ControlPanel/Images/CATreeView/small_icons/lines/"
	enableviewstate="false" ClientSideOnNodeMove="nodeMove" ClientSideOnNodeRename="nodeRename"
	ClientSideOnNodeMouseOut="checkNodeIndex" ClientSideOnNodeMouseOver="checkNodeIndex" ClientSideOnNodeSelect="nodeSelect"
	ExpandNodeOnSelect="true" CollapseNodeOnSelect="false" AutoPostBackOnSelect="false" AutoPostBackOnNodeMove="false" dropsiblingenabled="true"
	OnContextMenu="treeContextMenu" runat="server" MultipleSelectEnabled="False" BorderColor="#CCCCCC"
	BorderStyle="Solid" BorderWidth="0" FillContainer="True"></ca:treeview>
